<template>
  <div>
    <el-dialog
      title="关联商品"
      width="80%"
      :visible.sync="openGoods"
      :selectedProductList="selectedProductList"
      :close-on-click-modal="false"
      :before-close="cancel"
    >
      <div class="goods_con">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="商品分类" prop="oneCategoryId">
            <el-select
              v-model="queryParams.oneCategoryId"
              @change="getTwoCategory"
              placeholder="请选择分类"
            >
              <el-option
                v-for="item in deptOptions"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="twoCategoryId">
            <el-select
              v-model="queryParams.twoCategoryId"
              @change="getThreeCategory"
              placeholder="请选择二级分类"
            >
              <el-option
                v-for="item in twoCategory"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="threeCategoryId">
            <el-select
              v-model="queryParams.threeCategoryId"
              placeholder="请选择三级分类"
            >
              <el-option
                v-for="item in threeCategory"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品名称">
            <el-input
              v-model="queryParams.keyword"
              placeholder="请输入商品名称"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
          </el-form-item>
          <el-row :gutter="30">
            <el-col :span="12">
              <el-card class="box-card">
                <div slot="header" class="clearfix">
                  <span>待选择商品</span>
                </div>
                <el-table
                  v-loading="loading"
                  :data="productList"
                  @selection-change="handleSelectionChange"
                >
                  <el-table-column type="selection" width="50" align="center" />
                  <el-table-column
                    label="商品名称"
                    align="center"
                    prop="productName"
                  />
                  <el-table-column
                    label="图片"
                    align="center"
                    prop="previewImg"
                  >
                    <template slot-scope="scope">
                      <el-image
                        style="width: 100px; height: 100px"
                        :src="scope.row.previewImg"
                        fit="fit"
                        :preview-src-list="[scope.row.previewImg]"
                      ></el-image>
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="市场价格"
                    align="center"
                    prop="marketPrice"
                  />
                  <el-table-column
                    label="成本价格"
                    align="center"
                    prop="costPrice"
                  />
                  <el-table-column
                    label="售价"
                    align="center"
                    prop="productShopPrice"
                  />
                </el-table>

                <div style="text-align: right; margin-top: 15px">
                  <el-button type="primary" @click="checkProduct"
                    >选择</el-button
                  >
                </div>
              </el-card>
            </el-col>
            <el-col :span="12">
              <el-card class="box-card">
                <div slot="header" class="clearfix">
                  <span>已选择商品</span>
                </div>
                <el-table
                  v-loading="loading"
                  :data="selectedProductList"
                  @selection-change="handleDelOrSub"
                >
                  <el-table-column type="selection" width="50" align="center" />
                  <el-table-column
                    label="商品名称"
                    align="center"
                    prop="productName"
                  />
                  <el-table-column
                    label="图片"
                    align="center"
                    prop="previewImg"
                  >
                    <template slot-scope="scope">
                      <el-image
                        style="width: 100px; height: 100px"
                        :src="scope.row.previewImg"
                        fit="fit"
                        :preview-src-list="[scope.row.previewImg]"
                      ></el-image>
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="市场价格"
                    align="center"
                    prop="marketPrice"
                  />
                  <el-table-column
                    label="成本价格"
                    align="center"
                    prop="costPrice"
                  />
                  <el-table-column
                    label="售价"
                    align="center"
                    prop="productShopPrice"
                  />
                </el-table>
                <div style="text-align: right; margin-top: 15px">
                  <!-- <el-button type="primary" :disabled="multiple" @click="subProduct"
              >提交</el-button
            > -->
                  <el-button
                    type="danger"
                    :disabled="multiple"
                    @click="delProduct"
                    >移除</el-button
                  >
                </div>
              </el-card>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          :loading="subLoad"
          :disabled="multiple"
          type="primary"
          @click="submitForm"
          v-hasPermi="['cancel:coupon:showCouponProduct']"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { showLocalMallProduct, category } from "@/api/system/product";
export default {
  name: "RelatedGoods",
  props: {
    openGoods: {
      type: Boolean,
      default: false,
    },
    //选择后的商品列表
    selectedProductList: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      subLoad: false,
      queryParams: {
        oneCategoryId: null,
        twoCategoryId: null,
        threeCategoryId: null,
        mallId: null,
      },
      deptOptions: null,
      twoCategory: [],
      threeCategory: [],
      // 遮罩层
      loading: false,
      //待选择的商品列表
      productList: [],
      //选择状态下的商品列表
      middleProductList: [],
      //待删除或待提交的商品
      delProductList: [],
      goodsList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
    };
  },
  created() {
    this.getDeptTree();
  },
  watch: {
    openGoods(newVal, oldVal) {
      if (newVal) {
        this.reset();
      }
    },
  },
  methods: {
    // 表单重置
    reset() {
      this.productList = [];
      this.middleProductList = [];
      this.queryParams = {
        oneCategoryId: null,
        twoCategoryId: null,
        threeCategoryId: null,
        mallId: null,
      };
      this.resetForm("queryForm");
    },
    cancel() {
      this.$emit("close", false);
    },
    checkProduct() {
      const newArr = this.middleProductList.filter((v) =>
        this.selectedProductList.every((val) => val.productId != v.productId)
      );
      this.$emit("update:selectedProductList", [
        ...this.selectedProductList,
        ...newArr,
      ]);
      // this.selectedProductList = [...this.selectedProductList, ...newArr];
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection);
      this.middleProductList = selection;
    },
    //移除
    delProduct() {
      this.selectedProductList = this.selectedProductList.filter((v) =>
        this.delProductList.every((val) => v.productId != val.productId)
      );
      this.delProductList = [];
    },
    //多选
    handleDelOrSub(selection) {
      this.delProductList = selection;
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    getList() {
      this.loading = true;
      showLocalMallProduct(this.queryParams).then((res) => {
        console.log(res);
        this.productList = res.data;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      category().then((response) => {
        this.deptOptions = response.data;
      });
    },

    //获取二级分类
    getTwoCategory(e) {
      const twoCategory = this.deptOptions.find((v, i) => {
        return v.id == e;
      });
      this.twoCategory = twoCategory.children;
      console.log(this.twoCategory);
    },
    //获取三级分类
    getThreeCategory(e) {
      const threeCategory = this.twoCategory.find((v, i) => {
        return v.id == e;
      });
      this.threeCategory = threeCategory.children;
    },
    submitForm() {
      const data = {
        goodsIdArs: this.delProductList,
        ...this.queryParams,
      };
      this.$emit("getGoodsList", data);
    },
  },
};
</script>
<style scoped>
.goods_con {
  height: 600px;
  overflow-y: auto;
}
</style>
